package servlets.loginServlets;

import java.io.IOException;
import java.net.URL;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import managers.JsonManager;

import org.jsoup.Jsoup;

import structures.Seller;

public class ShowUserServlet extends HttpServlet {

	private static final long serialVersionUID = -3801859519441924766L;
	private static final String URLBASE = "http://localhost:8080/ISellToYouRESTWebService/ShowUser";
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response){
		String idUser, idAdvertisement, answer;
		URL url;
		JsonManager jsonManager;
		Seller seller;
		jsonManager = new JsonManager();
		idUser = request.getParameter("idUser");
		idAdvertisement = request.getParameter("idAdvertisement");
		
		try {
			url = new URL(URLBASE + "?idUser=" + idUser);
			answer = Jsoup.connect(url.toString()).ignoreContentType(true).execute().body();
			if(answer.equals("null"))
				throw new Exception();
			seller = jsonManager.toSeller(answer);
			request.getSession().setAttribute("seller", seller.getUser());
			request.getSession().setAttribute("idAdvertisement", idAdvertisement);
			request.getSession().setAttribute("Sold", seller.getSold());
			request.getSession().setAttribute("Available", seller.getAvailable());
			request.getSession().setAttribute("gamifyResult",seller.getGamifyResults());
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		try {
			response.sendRedirect(request.getContextPath() + "/Seller.jsp");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}

